package com.lzclass.service.adminservice.impl;

import com.lzclass.pojo.Article;
import com.lzclass.service.adminservice.UserService;
import com.lzclass.dao.admindao.UserMapper;
import com.lzclass.pojo.User;
import com.lzclass.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    User user = new User();

    @Autowired
    private UserMapper userMapper;
    @Override
    public User  login(String username, String password) {
        user.setUsername(username);
        user.setPassword(password);
        User user2 = userMapper.findUser(username, password);
        user2.setPassword("");
        if(user2!=null){

            user2.setToken(JwtUtil.getToken(user));
             return user2;
        }else{
            return null;
        }
    }

    @Override
    public String reg(String username, String password,String email) {

        if(userMapper.findUser(username,password)==null){
            userMapper.addUser(username,password,email);
            return "注册成功！";
        }
        return "注册失败！";
    }

    @Override
    public User findMessageByUid(int uid) {


        return userMapper.findMessageByUid(uid);
    }

    @Override
    public int changeMessageByUid(int uid, String email, String nickname) {


        return userMapper.changeMessageByUid(uid,email,nickname);
    }

    @Override
    public List<Article> findArticleByUid(int uid) {
        return userMapper.findArticleByUid(uid);
    }

    @Override
    public int delArticleByAid(int aid) {

        return userMapper.delArticleByAid(aid);
    }

    @Override
    public int updateHeadImg(int uid, String url) {

        return userMapper.updateUserImgByUid(uid,url);
    }

    @Override
    public int addArticleByUid(int uid, String title, String contexts, String type, String img, String time) {

        return userMapper.addArticleByUid(uid,title,contexts,type,img,time);
    }
}
